package com.ssm_basic.egweibo2.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ssm_basic.egweibo2.base.response.JsonResult;
import com.ssm_basic.egweibo2.mapper.CommentMapper;
import com.ssm_basic.egweibo2.pojo.dto.CommentAddParam;
import com.ssm_basic.egweibo2.pojo.entity.Comment;
import com.ssm_basic.egweibo2.pojo.vo.CommentVO;
import com.ssm_basic.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "3.评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发表评论")
    @ApiOperationSupport(order = 1)
    @PostMapping("insert")
    public JsonResult insert(CommentAddParam commentAddParam, @ApiIgnore HttpSession session) {
        log.debug("commentAddParam:{}", commentAddParam);
        UserVO  user = (UserVO) session.getAttribute("user");
        Comment comment = new Comment();
        comment.setUserId(user.getId());
        comment.setContent(commentAddParam.getContent());
        comment.setWeiboId(commentAddParam.getWeiboId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return JsonResult.success();
    }

    @ApiOperation("评论列表")
    @ApiOperationSupport(order = 5)
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(Long weiboId) {
        log.debug("wweiboId:" + weiboId);
        List<CommentVO> commentVO = commentMapper.selectByWeiboId(weiboId);
        return JsonResult.success(commentVO);
    }
}
